package com.yy.util.common;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Base64;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.UnsupportedEncodingException;

/**
 * Created by Administrator on 2015/6/12.
 */
public class SharedUtil {

    private static final String FILE_NAME = "app_shared";

    private static SharedPreferences mSharedPreference;
    private static SharedPreferences.Editor mEditor;

    public static void initSharedPreference(Context context) {
        mSharedPreference = context.getApplicationContext().getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        mEditor = mSharedPreference.edit();
    }

    /**
     * 保存数据的方法，我们需要拿到保存数据的具体类型，然后根据类型调用不同的保存方法
     * @param context
     * @param key
     * @param object
     */
    public static void put(Context context , String key, Object object){

        String type = object.getClass().getSimpleName();
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        switch (type) {
            case "String":
                editor.putString(key, (String)object);
                break;
            case "Integer":
                editor.putInt(key, (Integer)object);
                break;
            case "Boolean":
                editor.putBoolean(key, (Boolean)object);
                break;
            case "Float":
                editor.putFloat(key, (Float)object);
                break;
            case "Long":
                editor.putLong(key, (Long)object);
                break;
        }
        editor.commit();
    }

    /**
     * 得到保存数据的方法，我们根据默认值得到保存的数据的具体类型，然后调用相对于的方法获取值
     * @param context
     * @param key
     * @param defaultObject
     * @return
     */
    public static Object get(Context context , String key, Object defaultObject){
        String type = defaultObject.getClass().getSimpleName();
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);

        if("String".equals(type)){
            return sp.getString(key, (String)defaultObject);
        }
        else if("Integer".equals(type)){
            return sp.getInt(key, (Integer)defaultObject);
        }
        else if("Boolean".equals(type)){
            return sp.getBoolean(key, (Boolean)defaultObject);
        }
        else if("Float".equals(type)){
            return sp.getFloat(key, (Float)defaultObject);
        }
        else if("Long".equals(type)){
            return sp.getLong(key, (Long)defaultObject);
        }

        return null;
    }

    public static void clear(Context context) {
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
                Context.MODE_PRIVATE);
        SharedPreferences.Editor e = sp.edit();
        e.clear();
        e.commit();
    }

    public static void clearByKey(Context context, String key) {
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();
        edit.remove(key);
        edit.commit();
    }

    public static <T> void putObject(Context context, String key, T t) {
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();
        final ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream out = null;
        try {
            out = new ObjectOutputStream(bos);
            out.writeObject(t);
            byte[] data = bos.toByteArray();
            String value = Base64.encodeToString(data, 0, data.length, Base64.DEFAULT);
            mEditor.putString(key, value);
            mEditor.commit();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(bos != null) {
                    bos.close();
                }
                if(out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    public static <T> T getObject(Context context, String key) {
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
                Context.MODE_PRIVATE);
        if(mSharedPreference.contains(key)) {
            final String value = mSharedPreference.getString(key, null);
            byte[] buffer = null;
            buffer = Base64.decode(value.getBytes(), Base64.DEFAULT);
            final ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(buffer);
            ObjectInputStream input = null;
            try {
                input = new ObjectInputStream(arrayInputStream);
                T t = (T) input.readObject();
                return t;
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } finally {
                try {
                    if(arrayInputStream != null) {
                        arrayInputStream.close();
                    }
                    if(input != null) {
                        input.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();;
                }
            }
        }
        return null;
    }
}
